package com.dean.pokeking.ui.pages

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.expandVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.RadioButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowDown
import androidx.compose.material.icons.filled.KeyboardArrowUp
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

/**
 * @ClassName TODO
 * @Description TODO
 * @Author Dean
 * @Date 2023/3/29
 * @Version 1.0
 */

@Composable
fun SettingsPage(
    teamList: List<String>,
    selectTeamName: String,
    onTeamSelect: (teamName: String) -> Unit,
    reCopyExcels: () -> Unit
) {
    var teamExpanded by remember { mutableStateOf(false) }
    var reCopyExpanded by remember { mutableStateOf(false) }
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        item {
            SettingsCell(
                title = "选择队伍",
                isExpanded = teamExpanded
            ) { teamExpanded = !teamExpanded }
        }
        item {
            AnimatedVisibility(
                visible = teamExpanded,
                enter = expandVertically { 0 },
                exit = slideOutVertically { 0 }
            ) {
                Column(modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 10.dp)) {
                    teamList.forEach {teamName ->
                        TeamCell(
                            title = teamName,
                            isSelected = selectTeamName == teamName
                        ) {
                            onTeamSelect(teamName)
                        }
                    }
                }
            }
        }
        item {
            SettingsCell(
                title = "重置数据",
                isExpanded = reCopyExpanded
            ) { reCopyExpanded = !reCopyExpanded }
        }
        item {
            AnimatedVisibility(
                visible = reCopyExpanded,
                enter = expandVertically { 0 },
                exit = slideOutVertically { 0 }
            ) {
                Column(modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 10.dp)) {
                    Spacer(modifier = Modifier.height(5.dp))
                    Text(text = "确定要重置数据吗？这将删除所有未保存的自定义信息！！")
                    Spacer(modifier = Modifier.height(5.dp))
                    Button(
                        modifier = Modifier.fillMaxWidth(),
                        onClick = reCopyExcels
                    ) {
                        Text(text = "确定")
                    }
                }
            }
        }
    }
}

@Composable
fun SettingsCell(
    title: String = "",
    isExpanded: Boolean = false,
    onExpand: (Boolean) -> Unit = {}
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(50.dp)
            .clickable { onExpand(isExpanded) },
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Spacer(modifier = Modifier.width(8.dp))
        Text(text = title, fontSize = 25.sp)
        Spacer(modifier = Modifier.weight(1f))
        Icon(
            imageVector = if (!isExpanded) Icons.Filled.KeyboardArrowUp else Icons.Filled.KeyboardArrowDown,
            contentDescription = ""
        )
        Spacer(modifier = Modifier.width(8.dp))
    }
}


@Composable
fun TeamCell(
    title: String = "",
    isSelected: Boolean = false,
    onSelected: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .height(50.dp)
            .clickable { onSelected() },
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Spacer(modifier = Modifier.width(8.dp))
        RadioButton(selected = isSelected, onClick = { onSelected() })
        Text(text = title, fontSize = 25.sp)
    }
}

@Composable
@Preview(showBackground = true)
fun SettingsCellPreview() {
    SettingsCell("选择队伍")
}

@Composable
@Preview(showBackground = true)
fun TeamCellPreview() {
    TeamCell("小蛋队") {

    }
}

@Composable
@Preview(showBackground = true)
fun SettingsPagePreview() {
    var teamName by remember {
        mutableStateOf("小蛋队")
    }
    val teamList = listOf(
        "小蛋队",
        "笨蛋龙队",
        "鬼舞队",
    )
    SettingsPage(teamList = teamList, selectTeamName = teamName, onTeamSelect = { teamName = it }){}
}












